﻿using com.ruovea.gogs.entity;
using com.ruovea.utilty;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace com.ruovea.gogs.service
{
    public class BaseServer<T> where T : class, new()
    {
        #region Async

        #region 添加操作
        /// <summary>
        /// 添加一条数据
        /// </summary>
        /// <param name="parm">T</param>
        /// <returns></returns>
        public async Task<bool> AddAsync(T parm)
        {
            return await ctx.DB.GetSimpleClient<T>().InsertAsync(parm);
        }

        /// <summary>
        /// 批量添加数据
        /// </summary>
        /// <param name="parm">List<T></param>
        /// <returns></returns>
        public async Task<bool> AddListAsync(List<T> parm)
        {
            return await ctx.DB.GetSimpleClient<T>().InsertRangeAsync(parm.ToArray());
        }
        #endregion

        #region 查询操作
        /// <summary>
        /// 获得一条数据
        /// </summary>
        /// <param name="where">Expression<Func<T, bool>></param>
        /// <returns></returns>
        public async Task<T> GetModelAsync(Expression<Func<T, bool>> where)
        {
            return await ctx.DB.GetSimpleClient<T>().GetSingleAsync(where);
        }

        /// <summary>
        /// 获得一条数据
        /// </summary>
        /// <param name="parm">string</param>
        /// <returns></returns>
        public async Task<T> GetModelAsync(string parm)
        {
            return await ctx.DB.GetSimpleClient<T>().GetByIdAsync(parm);
        }

        /// <summary>
		/// 获得列表——分页
		/// </summary>
		/// <param name="parm">PageParm</param>
		/// <returns></returns>
        public async Task<List<T>> GetPagesAsync(Pagination parm)
        {

            return await ctx.DB.Queryable<T>()
                            .OrderByIF((!string.IsNullOrEmpty(parm.sidx)), parm.sidx + " " + parm.sord)
                            .ToPageAsync(parm);
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="parm">分页参数</param>
        /// <param name="where">条件</param>
        /// <returns></returns>
        public async Task<List<T>> GetPagesAsync(Pagination parm, Expression<Func<T, bool>> where)
        {

            Task<List<T>> query = ctx.DB.Queryable<T>()
                        .Where(where).OrderByIF(parm.sidx.NotNullOrWhiteSpace(), parm.sidx + " " + parm.sord)
                        .ToPageAsync(parm);

            return await query;
        }


        /// <summary>
		/// 获得列表
		/// </summary>
		/// <param name="parm">PageParm</param>
		/// <returns></returns>
        public async Task<List<T>> GetListAsync(Expression<Func<T, bool>> where)
        {
            var query = ctx.DB.Queryable<T>()
                    .Where(where)
                    .ToListAsync();
            return await query;
        }

        /// <summary>
		/// 获得列表
		/// </summary>
		/// <param name="parm">PageParm</param>
		/// <returns></returns>
        public async Task<List<T>> GetListAsync(Expression<Func<T, bool>> where, Expression<Func<T, object>> order)
        {
            var query = ctx.DB.Queryable<T>()
                    .Where(where)
                    .OrderByIF(true, order, OrderByType.Asc)
                 .ToListAsync();
            return await query;
        }

        /// <summary>
		/// 获得列表
		/// </summary>
		/// <param name="parm">PageParm</param>
		/// <returns></returns>
        public async Task<List<T>> GetListAsync(Expression<Func<T, bool>> where, Expression<Func<T, object>> order, SordEnum orderEnum)
        {
            var query = ctx.DB.Queryable<T>()
                    .Where(where)
                    .OrderByIF((int)orderEnum == 1, order, OrderByType.Asc)
                    .OrderByIF((int)orderEnum == 2, order, OrderByType.Desc)
                                 .ToListAsync();
            return await query;
        }

        /// <summary>
        /// 获得列表，不需要任何条件
        /// </summary>
        /// <returns></returns>
        public async Task<List<T>> GetListAsync()
        {
            var query = ctx.DB.Queryable<T>()
                                .ToListAsync();
            return await query;
        }
        #endregion

        #region 修改操作
        /// <summary>
        /// 修改一条数据
        /// </summary>
        /// <param name="parm">T</param>
        /// <returns></returns>
        public async Task<int> UpdateAsync(T parm)
        {
            var dbres = ctx.DB.Updateable<T>(parm).IgnoreColumns(true).ExecuteCommandAsync();
            return await dbres;
        }

        /// <summary>
        /// 修改一条数据，可用作假删除
        /// </summary>
        /// <param name="columns">修改的列=Expression<Func<T,T>></param>
        /// <param name="where">Expression<Func<T,bool>></param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(Expression<Func<T, T>> columns, Expression<Func<T, bool>> where)
        {
            var dbres = ctx.DB.GetSimpleClient<T>().UpdateAsync(columns, where);
            return await dbres;
        }
        #endregion

        #region 删除操作
        /// <summary>
        /// 删除一条或多条数据
        /// </summary>
        /// <param name="parm">string</param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(string parm)
        {
            var list = parm.StrToListString();
            var dbres = ctx.DB.GetSimpleClient<T>().DeleteByIdAsync(list.ToArray());
            return await dbres;
        }

        /// <summary>
        /// 删除一条或多条数据
        /// </summary>
        /// <param name="where">Expression<Func<T, bool>></param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(Expression<Func<T, bool>> where)
        {
            var dbres = ctx.DB.GetSimpleClient<T>().DeleteAsync(where);
            return await dbres;
        }

        /// <summary>
        /// 假删除
        /// </summary>
        /// <param name="parm">ID集合</param>
        /// <param name="columns">假删除列</param>
        /// <param name="where">条件</param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(Expression<Func<T, T>> columns, Expression<Func<T, bool>> where)
        {
            var dbres = ctx.DB.GetSimpleClient<T>().UpdateAsync(columns, where);
            return await dbres;
        }
        #endregion

        #endregion

        #region NoAsync

        #region 添加操作
        /// <summary>
        /// 添加一条数据
        /// </summary>
        /// <param name="parm">T</param>
        /// <returns></returns>
        public bool Add(T parm)
        {
            var dbres = ctx.DB.GetSimpleClient<T>().Insert(parm);
            return dbres;
        }

        /// <summary>
        /// 批量添加数据
        /// </summary>
        /// <param name="parm">List<T></param>
        /// <returns></returns>
        public bool AddList(List<T> parm)
        {
            var dbres = ctx.DB.GetSimpleClient<T>().InsertRange(parm.ToArray());
            return dbres;
        }
        #endregion

        #region 查询操作
        /// <summary>
        /// 获得一条数据
        /// </summary>
        /// <param name="where">Expression<Func<T, bool>></param>
        /// <returns></returns>
        public T GetModel(Expression<Func<T, bool>> where)
        {
            var model = ctx.DB.GetSimpleClient<T>().GetSingle(where);
            return model;
        }

        /// <summary>
        /// 获得一条数据
        /// </summary>
        /// <param name="parm">string</param>
        /// <returns></returns>
        public T GetModel(string parm)
        {
            var model = ctx.DB.GetSimpleClient<T>().GetById(parm);
            return model;
        }

        /// <summary>
		/// 获得列表——分页
		/// </summary>
		/// <param name="parm">PageParm</param>
		/// <returns></returns>
        public List<T> GetPages(Pagination parm)
        {

            List<T> query = ctx.DB.Queryable<T>()
                       .OrderByIF(parm.sidx.NotNullOrWhiteSpace(), parm.sidx + " " + parm.sord)
                       .ToPage(parm);
            return query;
        }

        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="parm">分页参数</param>
        /// <param name="where">条件</param>
        /// <returns></returns>
        public List<T> GetPages(Pagination parm, Expression<Func<T, bool>> where)
        {
            List<T> query = ctx.DB.Queryable<T>()
                        .Where(where).OrderByIF(parm.sidx.NotNullOrWhiteSpace(), parm.sidx + " " + parm.sord)
                        .ToPage(parm);
            return query;
        }



        /// <summary>
        /// 获得列表
        /// </summary>
        /// <param name="parm">PageParm</param>
        /// <returns></returns>
        public List<T> GetList(Expression<Func<T, bool>> where)
        {
            var query = ctx.DB.Queryable<T>()
                    .Where(where)
                    .ToList();
            return query;
        }

        /// <summary>
		/// 获得列表
		/// </summary>
		/// <param name="parm">PageParm</param>
		/// <returns></returns>
        public List<T> GetList(Expression<Func<T, bool>> where, Expression<Func<T, object>> order)
        {
            var query = ctx.DB.Queryable<T>()
                    .Where(where)
                    .OrderByIF(true, order, OrderByType.Asc)
                    .ToList();
            return query;
        }

        /// <summary>
		/// 获得列表
		/// </summary>
		/// <param name="parm">PageParm</param>
		/// <returns></returns>
        public List<T> GetList(Expression<Func<T, bool>> where, Expression<Func<T, object>> order, SordEnum orderEnum)
        {
            var query = ctx.DB.Queryable<T>()
                    .Where(where)
                    .OrderByIF((int)orderEnum == 1, order, OrderByType.Asc)
                    .OrderByIF((int)orderEnum == 2, order, OrderByType.Desc)
                    .ToList();
            return query;
        }

        /// <summary>
        /// 获得列表，不需要任何条件
        /// </summary>
        /// <returns></returns>
        public List<T> GetList()
        {
            var query = ctx.DB.Queryable<T>()
                    .ToList();
            return query;
        }
        #endregion

        #region 修改操作
        /// <summary>
        /// 修改一条数据
        /// </summary>
        /// <param name="parm">T</param>
        /// <returns></returns>
        public int Update(T parm)
        {
            var dbres = ctx.DB.Updateable<T>(parm).IgnoreColumns(true).RemoveDataCache().ExecuteCommand();
            return dbres;
        }

        /// <summary>
        /// 修改一条数据，可用作假删除
        /// </summary>
        /// <param name="columns">修改的列=Expression<Func<T,T>></param>
        /// <param name="where">Expression<Func<T,bool>></param>
        /// <returns></returns>
        public bool Update(Expression<Func<T, T>> columns, Expression<Func<T, bool>> where)
        {
            var dbres = ctx.DB.GetSimpleClient<T>().Update(columns, where);
            return dbres;
        }
        #endregion

        #region 删除操作
        /// <summary>
        /// 删除一条或多条数据
        /// </summary>
        /// <param name="parm">string</param>
        /// <returns></returns>
        public bool Delete(string parm)
        {
            var list = parm.StrToListString();
            var dbres = ctx.DB.GetSimpleClient<T>().DeleteByIds(list.ToArray());
            return dbres;
        }

        /// <summary>
        /// 删除一条或多条数据
        /// </summary>
        /// <param name="where">Expression<Func<T, bool>></param>
        /// <returns></returns>
        public bool Delete(Expression<Func<T, bool>> where)
        {
            var dbres = ctx.DB.GetSimpleClient<T>().Delete(where);

            return dbres;
        }

        /// <summary>
        /// 假删除
        /// </summary>
        /// <param name="parm">ID集合</param>
        /// <param name="columns">假删除列</param>
        /// <param name="where">条件</param>
        /// <returns></returns>
        public bool Delete(Expression<Func<T, T>> columns, Expression<Func<T, bool>> where)
        {
            var dbres = ctx.DB.GetSimpleClient<T>().Update(columns, where);
            return dbres;
        }
        #endregion 

        #endregion
    }
}
